package com.vm.osm.business.controller;

import com.vm.osm.business.common.Constants;
import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.common.ErrCode;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.req.checkPlan.ReqAddCheckPlan;
import com.vm.osm.business.dto.req.checkPlan.ReqAddPlanContent;
import com.vm.osm.business.dto.req.checkPlan.ReqUpdateCheckPlan;
import com.vm.osm.business.dto.req.checkPlan.ReqUpdatePlanContent;
import com.vm.osm.business.dto.rsp.checkPlan.*;
import com.vm.osm.business.dto.rsp.orderInst.RspOrderPage;
import com.vm.osm.business.entity.OsmCheckPlan;
import com.vm.osm.business.service.ICheckPlanInstService;
import com.vm.osm.business.service.ICheckPlanService;
import com.vm.osm.business.util.DateFormatUtils;
import com.vm.osm.business.util.UserDataUtil;
import com.vm.osm.business.util.UuidUtil;
import com.vma.eams.spring.boot.annotation.LogFunc;
import com.vma.eams.spring.boot.annotation.LogModule;
import com.vma.eams.spring.boot.log.LogCollector;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2018/2/7.
 */
@RestController
@RequestMapping("/v1.0")
@LogModule(type = "loop",name = "巡检计划")
@Api(value = "LoopTaskController",description = "巡检计划",tags = {"LoopTaskController"})
public class LoopTaskController extends ControllerExceptionHandler {
    private Logger LOG = Logger.getLogger(this.getClass());

    @Value("${eam_api_url}")
    private String eamApiUrl;

    @Autowired
    private ICheckPlanService checkPlanService;

    @Autowired
    ICheckPlanInstService checkPlanInstService;

    @Autowired
    private RestTemplate restTemplate;
    /**
     * 新增巡检单
     * @return
     */
    @LogModule(type = "create",name = "创建巡检计划")
    @ApiOperation(value = "新增巡检单",notes = "新增巡检单")
    @ApiImplicitParam(name = "reqAddCheckPlan",value = "新增巡检单参数",required = true,dataType = "ReqAddCheckPlan")
    @RequestMapping(value = "/add_check_plan",method = RequestMethod.POST)
    public void save(@RequestBody ReqAddCheckPlan reqAddCheckPlan,HttpServletRequest request){
        LOG.info("新增巡检单");
        String enterpriseId= UserDataUtil.getUserSelectedEnterpriseId(request);
        String staffName = UserDataUtil.getStaffName(request);
        String staffId = UserDataUtil.getStaffId(request);
        String phone = UserDataUtil.getStaffPhone(request);

        reqAddCheckPlan.setStaff_name(staffName);
        reqAddCheckPlan.setStaff_id(staffId);
        reqAddCheckPlan.setEnterprise_id(enterpriseId);
        reqAddCheckPlan.setPhone(phone);
        checkPlanService.createCheckPlan(reqAddCheckPlan);
    }

    /**
     * 修改巡检单信息
     * @param reqUpdateCheckPlan
     * @return
     */
    @LogFunc(type = "edit",name = "编辑巡检计划")
    @ApiOperation(value = "修改巡检单信息",notes = "修改巡检单信息")
    @ApiImplicitParam(name = "reqUpdateCheckPlan",value = "修改巡检单请求参数",required = true,dataType = "ReqUpdateCheckPlan")
    @RequestMapping(value = "/update_check_info",method = RequestMethod.PUT)
    public void updateCheckPlan(@RequestBody ReqUpdateCheckPlan reqUpdateCheckPlan,HttpServletRequest request){
        LOG.info("修改巡检单信息");
        checkPlanService.updateCheckPlan(reqUpdateCheckPlan,request);

    }

    @ApiOperation(value = "根据ID查询巡检单",notes = "根据ID查询巡检单，用户修改前的数据初始化")
    @RequestMapping(value = "/get_check_plan_by_id/{id}",method = RequestMethod.GET)
    public RspCheckPlan getCheckPlanById(
            @ApiParam(required=true, name="id", value="工单ID") @PathVariable("id") String id){
        return checkPlanService.selectCheckPlanById(id);
    }
    @LogFunc(type = "action",name = "暂停与删除")
    @ApiOperation(value = "巡检单暂停与删除",notes = "巡检单暂停、删除、发布")
    @RequestMapping(value = "/check/handle_action",method = RequestMethod.PUT)
    public void handCheckPlanAction(
            @ApiParam(required = true,name = "type",value = "动作类型 1暂停 2删除 3发布") @RequestParam( "type") String type,
            @ApiParam(required = true,name = "id",value = "巡检计划ID") @RequestParam("id") String id){
        checkPlanService.handCheckPlanAction(type,id);
    }



    /**
     * 获取巡检列表信息
     * @return
     */
    @ApiOperation(value = "获取巡检列表信息",notes = "获取巡检列表信息")
    @RequestMapping(value = "/checks",method = RequestMethod.GET)
    public RspCheckPlanPage getCheckPlans(
             @ApiParam(required=true, name="page_num", value="分页数") @RequestParam("page_num") Integer page_num,
             @ApiParam(required=true, name="page_size", value="分页大小") @RequestParam("page_size") Integer page_size,
             @ApiParam(name="title", value="巡检标题") @RequestParam(value = "title",required = false) String title,
             @ApiParam(name="plan_type", value="计划类型 1一次性2按天3按周4按月5按年")
             @RequestParam(value = "plan_type",required = false) String plan_type,
             @ApiParam( name="status", value="巡检单状态，草稿、未开始、进行中、已结束、已终止") @RequestParam(value = "status",required=false) String status,
             @ApiParam( name="start_time", value="开始时间") @RequestParam(value = "start_time",required=false) Date start_time,
             @ApiParam( name="end_time", value="结束时间") @RequestParam(value = "end_time",required=false) Date end_time,HttpServletRequest request){
        LOG.info("获取巡检列表信息");
        RspCheckPlanPage rspCheckPlanPage = checkPlanService.findPage(UserDataUtil.getUserSelectedEnterpriseId(request),
                page_num,page_size,title,plan_type,status,start_time,end_time);
        return rspCheckPlanPage;
    }

    @LogFunc(type = "create",name = "新增巡检内容")
    @ApiOperation(value = "新增巡检内容",notes = "新增巡检内容")
    @ApiImplicitParam(name = "reqAddPlanContent",value = "新增巡检内容请求参数",required = true,dataType = "ReqAddPlanContent")
    @RequestMapping(value = "save_plan",method = RequestMethod.POST)
    public void savePlan(@RequestBody ReqAddPlanContent reqAddPlanContent, HttpServletRequest request){
        LOG.info("新增巡检内容");

        String staffId = UserDataUtil.getStaffId(request);

        String staffName = UserDataUtil.getStaffName(request);

        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);

        checkPlanService.addCheckContent(reqAddPlanContent,staffId,staffName,enterpriseId);
    }

    @ApiOperation(value = "巡检内容列表",notes = "巡检内容列表")
    @RequestMapping(value = "/task/find_content_by_page",method = RequestMethod.GET)
    public RspPlanContentPage findContentByPage(
            @ApiParam(required=true, name="page_num", value="分页数") @RequestParam("page_num") Integer pageNum,
            @ApiParam(required=true, name="page_size", value="分页大小") @RequestParam("page_size") Integer pageSize,
            @ApiParam(name="plan_title", value= "巡检内容标题") @RequestParam(value = "plan_title",required = false) String planTitle,HttpServletRequest request){
        return checkPlanService.findContentByPage(pageNum,pageSize,planTitle,UserDataUtil.getUserSelectedEnterpriseId(request));
    }

    @LogFunc(type = "delete",name = "删除巡检内容")
    @ApiOperation(value = "删除巡检内容",notes = "删除巡检内容,多个巡检内容ID用英文逗号隔开")
    @RequestMapping(value = "/task/del_contents",method = RequestMethod.DELETE)
    public void deleteContent(
            @ApiParam(value = "ids",name="ids",required = true) @RequestParam("ids") String ids){
        LOG.info("删除巡检内容");
        checkPlanService.deleteContentByIds(ids);
    }

    @ApiOperation(value = "获取巡检内容详情",notes = "根据ID获取巡检内容详情")
    @RequestMapping(value = "/task/get_content_by_id/{id}",method = RequestMethod.GET)
    public RspContentDetail getContentById(@ApiParam(value = "id",name = "id",required = true)
                               @PathVariable("id") String id){
        LOG.info("获取巡检内容详情");
        return checkPlanService.getContentById(id);
    }

    @LogFunc(type = "update",name = "编辑巡检内容")
    @ApiOperation(value = "更新巡检内容",notes = "更新巡检内容")
    @ApiImplicitParam(name = "reqUpdatePlanContent",value = "更新巡检内容请求参数",required = true,dataType = "ReqUpdatePlanContent")
    @RequestMapping(value = "update_plan",method = RequestMethod.POST)
    public void updatePlanContent(@RequestBody ReqUpdatePlanContent reqUpdatePlanContent, HttpServletRequest request){
        LOG.info("更新巡检内容");

        String staffId = UserDataUtil.getStaffId(request);

        String staffName = UserDataUtil.getStaffName(request);

        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);

        checkPlanService.updateCheckContent(reqUpdatePlanContent,staffId,staffName,enterpriseId);
    }

    @ApiOperation(value = "查询设备关联的KPI",notes = "查询设备关联的KPI")
    @ApiImplicitParam(name = "equ_list",value = "equ_list",dataType = "List<EquTypeKpis>",required = true)
    @RequestMapping(value = "/task/kpi_by_equ",method = RequestMethod.PUT)
    public List<EquTypeKpis> queryKpiByEquType(
            HttpServletRequest request,
            @RequestBody List<EquTypeKpis> equ_list){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        return checkPlanService.queryKpiByEquType(enterpriseId,equ_list);
    }

    @ApiOperation(value = "查询服务商",notes = "http://120.55.12.229:9001/eam/swagger-ui.html#!/EquInfoController/getEnterpriseEquProductTypeUsingGET")
    @RequestMapping(value = "/task/service_provider/list",method = RequestMethod.GET)
    public Object serviceProvider(HttpServletRequest request){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        String postUrl = eamApiUrl+"/eam/v1.0/"+enterpriseId+"/service_providers";
        Object entity = restTemplate.getForEntity(postUrl,Object.class).getBody();
        return entity;
    }


    @ApiOperation(value = "根据巡检计划查询巡检任务",notes = "根据巡检计划查询巡检任务")
    @RequestMapping(value = "/task/list/{id}",method = RequestMethod.GET)
    public RspOrderPage queryTaskByPlan(
            @ApiParam(required=true, name="page_num", value="分页数") @RequestParam("page_num") Integer pageNum,
            @ApiParam(required=true, name="page_size", value="分页大小") @RequestParam("page_size") Integer pageSize,
            @ApiParam(name="id", value= "巡检计划ID") @PathVariable(value = "id") String id){
        return checkPlanInstService.queryLoops(id,pageNum,pageSize);
    }

}


